package com.iespring.util.configmanager;

import com.iespring.bean.admin.user.vo.ConfigMallVo;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * \* Auth: 杨宇健
 * \* Time: 2022/12/31 10:05
 * \* Description: 配置管理实体的工具类
 * \
 */
public class ConfigManagerUtils {

    private ConfigManagerUtils() {
    }
    //
    //public static void main(String[] args) {
    //    ConfigMallVo configMallVo = new ConfigMallVo();
    //    configMallVo.setMarket_mall_address("1234454");
    //    configMallVo.setMarket_mall_latitude("52465");
    //    System.out.println(getMaps(configMallVo));
    //    System.out.println(getKerValueBean(configMallVo));
    //}

    /**
     * 返回的是一个String: String Map
     *
     * @param o
     * @return java.util.List<com.iespring.util.configmanager.ConfigKeyValue>
     * @author 杨宇健
     * @since 2022/12/31 10:40
     */
    public static HashMap<String, String> getMaps(Object o) {
        HashMap<String, String> map = new HashMap<>();
        Class<?> aClass = o.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields) {
            //获得修饰
            int modifiers = field.getModifiers();
            //静态变量不用管
            if (Modifier.isStatic(modifiers)) {
                continue;
            }
            field.setAccessible(true);
            Object value = null;
            try {
                value = field.get(o);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //null值不入Map
            if (value == null) {
                continue;
            }
            String resValue = "";
            if (value.getClass().getSimpleName() != "String") {
                resValue = String.valueOf(value);
            }
            map.put(field.getName(), resValue);
        }
        return map;
    }

    /**
     * 返回 key value格式的对象，都是字符串
     *
     * @param o
     * @return java.util.List<com.iespring.util.configmanager.ConfigKeyValue>
     * @author 杨宇健
     * @since 2022/12/31 11:00
     */
    public static List<ConfigKeyValue> getKeyValueList(Object o) {
        HashMap<String, String> maps = getMaps(o);
        Set<Map.Entry<String, String>> entries = maps.entrySet();
        List<ConfigKeyValue> keyValues = entries.stream().map(
                map -> {
                    ConfigKeyValue configKeyValue = new ConfigKeyValue();
                    configKeyValue.setKey(map.getKey());
                    configKeyValue.setValue(map.getValue());
                    return configKeyValue;
                }).collect(Collectors.toList());
        return keyValues;
    }

    //将ConfigKeyValue变成 String, String 的 map
    public static Map<String, String> listToMap(List<ConfigKeyValue> list){
        HashMap<String, String> map = new HashMap<>();
        list.stream().forEach( l -> {
            map.put(l.getKey(), l.getValue());
        });
        return map;
    }
}
